// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import Box from "./Box";

const { ccclass, property } = cc._decorator;

enum ColliderType {
    INNER,
    OTHER,
}

@ccclass
export default class ColliderGenerator extends cc.Component {
    @property(cc.Node)
    monster: cc.Node = null;

    @property(cc.Node)
    bullet: cc.Node = null;

    @property({
        type: cc.Enum(ColliderType)
    })
    colliderType: ColliderType = ColliderType.INNER;

    public monsters: Box[] = [];
    public bullets: Box[] = [];
    private _monsterCount: number = 2000;
    private _bulletCount: number = 100;
    private _monsterSize: cc.Size = cc.size(10, 50);
    private _bulletSize: cc.Size = cc.size(10, 50);

    start() {
        for (let i = 0, len = this._monsterCount; i < len; i++) {
            let node = cc.instantiate(this.monster);
            let box = node.getComponent(Box);
            node.parent = this.node;
            node.x = Math.random() * cc.winSize.width - cc.winSize.width * 0.5;
            node.y = Math.random() * cc.winSize.height - cc.winSize.height * 0.5;
            node.width = node.height = this._monsterSize.width + Math.random() * (this._monsterSize.height - this._monsterSize.width);
            this.monsters.push(box);
        }

        for (let i = 0, len = this._bulletCount; i < len; i++) {
            let node = cc.instantiate(this.bullet);
            let box = node.getComponent(Box);
            node.parent = this.node;
            node.x = Math.random() * cc.winSize.width - cc.winSize.width * 0.5;
            node.y = Math.random() * cc.winSize.height - cc.winSize.height * 0.5;
            node.width = node.height = this._bulletSize.width + Math.random() * (this._bulletSize.height - this._bulletSize.width);
            this.bullets.push(box);
        }

        switch (this.colliderType) {
            case ColliderType.INNER:
                cc.director.getCollisionManager().enabled = true;
                break;
        }

        
    }
}
